package com.bgc.codegenerate.utils;

import com.bgc.codegenerate.CodeGenerate;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class Converter {

    public static void main(String[] args) {
        //testing for data base column-key to java field

        String entityName = new Converter().convertToJava("ofa_temp2");
        entityName = entityName.substring(0, 1).toUpperCase() + entityName.substring(1);
        log.info(entityName);

        String[] dbKeys = {"id", "user_age", "user_addr_", "ofa_temp"};
        Converter t = new Converter();
        t.convertToJava(dbKeys);
        log.info("-----------------------------------");
        //testing for Java field to data base column-key
        String javaFieldNames[] = {"id", "userAge", "userHomeAddr"};
        t.getDBKey(javaFieldNames);
    }


    /*
     * Java field to data base column-key
     */

    private void getDBKey(String... javaFieldNames) {
        if (javaFieldNames != null && javaFieldNames.length > 0) {
            for (String name : javaFieldNames) {
                StringBuilder stringBuilder = new StringBuilder();
                char[] array = name.toCharArray();
                List<Integer> insertIndexes = new ArrayList<>();
                for (int i = 0; i < array.length; i++) {
                    Character c = array[i];
                    if (i != 0 && Character.isUpperCase(c)) {
                        insertIndexes.add(i);
                    }
                }
                if (insertIndexes.size() > 0) {
                    int flag = 0;
                    for (int j = 0; j < insertIndexes.size(); j++) {
                        String word = toLowercase4FirstLetter(name.substring(flag, insertIndexes.get(j)));
                        stringBuilder.append(word).append("_");
                        flag = insertIndexes.get(j);
                    }
                    String last = toLowercase4FirstLetter(name.substring(flag));
                    stringBuilder.append(last);
                    log.info(stringBuilder.toString());
                } else {
                    log.info(name);
                }
            }
        }
    }

    private String toLowercase4FirstLetter(String word) {
        if (word != null && word.length() > 0) {
            String firstLetter = word.substring(0, 1);
            String others = word.substring(1);
            return firstLetter.toLowerCase() + others;
        } else {
            return "";
        }
    }

    /*
     * data base column-key to java field
     */
    public String convertToJava(String... dbKeys) {
        String result = "";
        if (dbKeys != null && dbKeys.length > 0) {
            for (String key : dbKeys) {
                if (!StringUtils.isBlank(CodeGenerate.TABLE_PREFIX)) {
                    key = StringUtils.substringAfter(key, CodeGenerate.TABLE_PREFIX);
                }
                String[] words = key.split("_");
//                String[] entityName = new String[words.length -1];
//                
//                for (int i =1 ; i < words.length ; i ++) {
//                	entityName[i - 1] = words[i];
//                }
                result = toUppercase4FirstLetter(words);
            }
        }
        return result;
    }

    public String toUppercase4FirstLetter(String... words) {
        StringBuffer buffer = new StringBuffer();
        if (words != null && words.length > 0) {
            for (int i = 0; i < words.length; i++) {
                String word = words[i];
                String firstLetter = word.substring(0, 1);
                String others = word.substring(1);
                String upperLetter = null;
                if (i != 0) {
                    upperLetter = firstLetter.toUpperCase();
                } else {
                    upperLetter = firstLetter;
                }
                buffer.append(upperLetter).append(others);
            }
            return buffer.toString();
        }
        return "";
    }


}